<?php
/*
Template Name: Portfolio Page
*/
?>
<?php
$realestate_page_template_settings = array();
$realestate_page_template_settings = maybe_unserialize( get_post_meta($post->ID,'realestate_page_template_settings',true) );

$fullwidth = isset( $realestate_page_template_settings['realestate_fullwidthpage'] ) ? (bool) $realestate_page_template_settings['realestate_fullwidthpage'] : false;
$realestate_page_template_showtitle = isset( $realestate_page_template_settings['realestate_page_template_showtitle'] ) ? (bool) $realestate_page_template_settings['realestate_page_template_showtitle'] : false;
$realestate_page_template_showdesc = isset( $realestate_page_template_settings['realestate_page_template_showdesc'] ) ? (bool) $realestate_page_template_settings['realestate_page_template_showdesc'] : false;
$realestate_page_template_detect_portrait = isset( $realestate_page_template_settings['realestate_page_template_detect_portrait'] ) ? (bool) $realestate_page_template_settings['realestate_page_template_detect_portrait'] : false;

$gallery_cats = isset( $realestate_page_template_settings['realestate_page_template_gallerycats'] ) ? (array) $realestate_page_template_settings['realestate_page_template_gallerycats'] : array();
$realestate_page_template_gallery_perpage = isset( $realestate_page_template_settings['realestate_page_template_gallery_perpage'] ) ? (int) $realestate_page_template_settings['realestate_page_template_gallery_perpage'] : 12;

$realestate_page_template_portfolio_size = isset( $realestate_page_template_settings['realestate_page_template_imagesize'] ) ? (int) $realestate_page_template_settings['realestate_page_template_imagesize'] : 2;

$realestate_page_template_portfolio_class = '';
if ( $realestate_page_template_portfolio_size == 1 ) $realestate_page_template_portfolio_class = ' realestate_portfolio_small';
if ( $realestate_page_template_portfolio_size == 3 ) $realestate_page_template_portfolio_class = ' realestate_portfolio_large';
?>
<?php get_header(); ?>
	<div id="content-top">
		<div id="menu-bg"></div>
		<div id="top-index-overlay"></div>
		<div id="content" class="clearfix<?php if($fullwidth) echo(' fullwidth');?>">
			<div id="main-area">
				<?php get_template_part('includes/breadcrumbs'); ?>
				<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
				<div class="full_entry clearfix">
					<?php if (get_option('realestate_integration_single_top') <> '' && get_option('realestate_integrate_singletop_enable') == 'on') echo(get_option('realestate_integration_single_top')); ?>
					<?php $width = 159;
						  $height = 159;
						  $classtext = '';
						  $titletext = get_the_title();
						  $thumbnail = get_thumbnail($width, $height, $classtext, $titletext, $titletext);
						  $thumb = $thumbnail["thumb"]; ?>
					<div class="entry_content<?php if ($thumb <> '' && get_option('realestate_thumbnails_index') == 'on') echo(' setwidth') ?>">
						<?php if($thumb <> '' && get_option('realestate_page_thumbnails') == 'on') {?>
							<div class="small-thumb">
								<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
								<span class="overlay"></span>
							</div><!-- end .small-thumb -->
						<?php }; ?>
						<h1 class="single-title"><?php the_title(); ?></h1>
						<?php the_content(); ?>
						<?php wp_link_pages(array('before' => '<p><strong>'.esc_html__('Pages', 'RealEstate').':</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
						<div id="realestate_page_template_portfolio_gallery" class="clearfix<?php echo $realestate_page_template_portfolio_class; ?>">
							<?php $gallery_query = '';
							$portfolio_count = 1;
							$realestate_open_row = false;
							if ( !empty($gallery_cats) ) $gallery_query = '&cat=' . implode(",", $gallery_cats);
							else echo '<!-- gallery category is not selected -->'; ?>
							<?php
								global $wp_embed;
								$realestate_videos_output = '';
								$realestate_paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' );
							?>
							<?php query_posts('showposts=' . $realestate_page_template_gallery_perpage . '&paged=' . $realestate_paged . $gallery_query); ?>
							<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
								<?php $width = 260;
								$height = 170;
								if ( $realestate_page_template_portfolio_size == 1 ) {
									$width = 140;
									$height = 94;
									$realestate_portrait_height = 170;
								}
								if ( $realestate_page_template_portfolio_size == 2 ) $realestate_portrait_height = 315;
								if ( $realestate_page_template_portfolio_size == 3 ) {
									$width = 430;
									$height = 283;
									$realestate_portrait_height = 860;
								}
								$realestate_auto_image_detection = false;
								if ( has_post_thumbnail( $post->ID ) && $realestate_page_template_detect_portrait ) {
									$wordpress_thumbnail = get_post( get_post_thumbnail_id($post->ID) );
									$wordpress_thumbnail_url = $wordpress_thumbnail->guid;
									if ( realestate_is_portrait($wordpress_thumbnail_url) ) $height = $realestate_portrait_height;
								}
								$titletext = get_the_title();
								$realestate_portfolio_title = get_post_meta($post->ID,'realestate_portfolio_title', true) ? get_post_meta($post->ID,'realestate_portfolio_title', true) : get_the_title();
								$realestate_videolink = get_post_meta($post->ID,'realestate_videolink', true) ? get_post_meta($post->ID,'realestate_videolink', true) : '';
								if ( '' != $realestate_videolink ){
									$realestate_video_id = 'realestate_video_post_' . $post->ID;
									$realestate_videos_output .= '<div id="'. esc_attr( $realestate_video_id ) .'">' . $wp_embed->shortcode( '', $realestate_videolink ) . '</div>';
								}
								$thumbnail = get_thumbnail($width, $height, '', $titletext, $titletext, true, 'realestate_portfolio');
								$thumb = $thumbnail["thumb"];
								if ( $realestate_page_template_detect_portrait && $thumbnail["use_timthumb"] && realestate_is_portrait($thumb) ) {
									$height = $realestate_portrait_height;
								} ?>
								<?php if ( $portfolio_count == 1 || ( $realestate_page_template_portfolio_size == 2 && (!$fullwidth && ($portfolio_count+1) % 2 == 0) ) || ( $realestate_page_template_portfolio_size == 3 && (($portfolio_count+1) % 2 == 0) ) ) {
									$realestate_open_row = true; ?>
									<div class="realestate_page_template_portfolio_row clearfix">
								<?php } ?>
										<div class="realestate_page_template_portfolio_item">
											<?php if ($realestate_page_template_showtitle) { ?>
												<h2 class="realestate_page_template_portfolio_title"><?php echo $realestate_portfolio_title; ?></h2>
											<?php } ?>
											<div class="realestate_page_template_portfolio_entry<?php if ( $height == $realestate_portrait_height ) echo ' realestate_portrait_layout'; ?>">
												<div class="realestate_page_template_portfolio_image<?php if ($realestate_videolink <> '') echo ' realestate_video'; ?>">
													<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, ''); ?>
													<span class="realestate_page_template_portfolio_overlay"></span>
													<a class="realestate_portfolio_zoom_icon fancybox" title="<?php the_title(); ?>"<?php if ($realestate_videolink == '') echo ' rel="portfolio"'; ?> href="<?php if ($realestate_videolink <> '') echo esc_url( '#' . $realestate_video_id ); else echo($thumbnail['fullpath']); ?>"><?php esc_html_e('Zoom in', 'RealEstate'); ?></a>
													<a class="realestate_portfolio_more_icon" href="<?php the_permalink(); ?>"><?php esc_html_e('Read more', 'RealEstate'); ?></a>
												</div><!-- end .realestate_page_template_portfolio_image -->
											</div><!-- end .realestate_page_template_portfolio_entry -->
											<?php if ($realestate_page_template_showdesc) { ?>
												<p><?php truncate_post(90); ?></p>
											<?php } ?>
										</div><!-- end .realestate_page_template_portfolio_item -->
								<?php if ( ($realestate_page_template_portfolio_size == 2 && !$fullwidth && $portfolio_count % 2 == 0) || ( $realestate_page_template_portfolio_size == 3 && ($portfolio_count % 2 == 0) ) ) {
									$realestate_open_row = false; ?>
									</div><!-- end .realestate_page_template_portfolio_row -->
								<?php } ?>
								<?php if ( ($realestate_page_template_portfolio_size == 2 && $fullwidth && $portfolio_count % 3 == 0) || ($realestate_page_template_portfolio_size == 1 && !$fullwidth && $portfolio_count % 3 == 0) || ($realestate_page_template_portfolio_size == 1 && $fullwidth && $portfolio_count % 5 == 0) ) { ?>
									</div><!-- end .realestate_page_template_portfolio_row -->
									<div class="realestate_page_template_portfolio_row clearfix">
									<?php $realestate_open_row = true; ?>
								<?php } ?>
							<?php $portfolio_count++;
							endwhile; ?>
								<?php if ( $realestate_open_row ) {
									$realestate_open_row = false; ?>
									</div><!-- end .realestate_page_template_portfolio_row -->
								<?php } ?>
								<div class="page-nav clearfix">
									<?php if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
									else { ?>
										<?php get_template_part('includes/navigation'); ?>
									<?php } ?>
								</div><!-- end .entry -->
							<?php else : ?>
								<?php if ( $realestate_open_row ) {
									$realestate_open_row = false; ?>
									</div><!-- end .realestate_page_template_portfolio_row -->
								<?php } ?>
								<?php get_template_part('includes/no-results'); ?>
							<?php endif; wp_reset_query(); ?>
							<?php if ( $realestate_open_row ) {
								$realestate_open_row = false; ?>
								</div><!-- end .realestate_page_template_portfolio_row -->
							<?php } ?>
							<?php if ( '' != $realestate_videos_output ) echo '<div class="realestate_embedded_videos">' . $realestate_videos_output . '</div>'; ?>
						</div><!-- end #realestate_page_template_portfolio_gallery -->
						<?php edit_post_link(esc_html__('Edit this page', 'RealEstate')); ?>
					</div><!-- end .entry_content -->
				</div><!-- .full_entry -->
				<?php if (get_option('realestate_show_pagescomments') == 'on') comments_template('', true); ?>
				<?php endwhile; endif; ?>
			</div><!-- end #main-area -->
			<?php if (!$fullwidth) get_sidebar(); ?>
<?php get_footer(); ?>